﻿using Nasty.Core.Session;
using Nasty.Core.Entity;
using SqlSugar;
using Nasty.Core.Entity.System;
using Nasty.Core.Attributes;
using Newtonsoft.Json;

namespace Nasty.Core.Entity.Flow
{
    [SugarTable("SysFlowNode", TableDescription = "流程节点")]
    public class FlowNode : StandardEntity<FlowNode>
    {

        /// <summary>
        /// 流程Id
        /// </summary>
        [NastyColumn(ColumnName = "FlowId", IsOnlyIgnoreUpdate = true, Required = true, ColumnDescription = "流程Id")]
        public string? FlowId { get; set; }

        /// <summary>
        /// 配置节点Id
        /// </summary>
        [NastyColumn(ColumnName = "ConfigNodeId", ColumnDescription = "配置节点Id", Required = true, IsOnlyIgnoreUpdate = true)]
        public string? ConfigNodeId { get; set; }

        /// <summary>
        /// 名称 - 同步配置节点
        /// </summary>
        [NastyColumn(ColumnName = "Name", ColumnDescription = "名称", Required = true, IsOnlyIgnoreUpdate = true)]
        public string? Name { get; set; }

        /// <summary>
        /// 编码 - 同步配置节点
        /// </summary>
        [NastyColumn(ColumnName = "Code", ColumnDescription = "编码", Required = true, IsOnlyIgnoreUpdate = true)]
        public string? Code { get; set; }


        /// <summary>
        /// 上一个节点Codes  - 同步配置节点
        /// </summary>
        [NastyColumn(ColumnName = "PreNodeCodes", IsJson = true, IsNullable = true, ColumnDescription = "上一个节点Codes", ColumnDataType = StaticConfig.CodeFirst_BigString)]
        public List<string> PreNodeCodes { get; set; }

        /// <summary>
        /// 驳回节点Code  - 同步配置节点
        /// </summary>
        [NastyColumn(ColumnName = "RejectNodeCode", IsNullable = true, ColumnDescription = "驳回节点Code")]
        public string? RejectNodeCode { get; set; }


        /// <summary>
        /// 候选模式
        /// </summary>
        [SugarColumn(ColumnName = "CandidateMode", ColumnDescription = "候选模式", IsOnlyIgnoreUpdate = true)]
        public CandidateMode CandidateMode { get; set; }

        /// <summary>
        /// 预选用户Ids
        /// </summary>
        [SugarColumn(ColumnName = "PreUserIds", IsJson = true, IsNullable = true, ColumnDescription = "预选用户Ids", IsOnlyIgnoreUpdate = true, ColumnDataType = StaticConfig.CodeFirst_BigString)]
        public List<string> PreUserIds { get; set; }

        /// <summary>
        /// 角色Ids
        /// </summary>
        [SugarColumn(ColumnName = "RoleIds", IsJson = true, IsNullable = true, ColumnDescription = "角色Ids", IsOnlyIgnoreUpdate = true, ColumnDataType = StaticConfig.CodeFirst_BigString)]
        public List<string> RoleIds { get; set; }


        /// <summary>
        /// 部门Ids
        /// </summary>
        [SugarColumn(ColumnName = "DepartmentIds", IsJson = true, IsNullable = true, ColumnDescription = "部门Ids", IsOnlyIgnoreUpdate = true, ColumnDataType = StaticConfig.CodeFirst_BigString)]
        public List<string> DepartmentIds { get; set; }




        /// <summary>
        /// 审批人Ids
        /// </summary>
        [NastyColumn(ColumnName = "ApproverIds", IsJson = true, IsNullable = true, ColumnDescription = "审批人Ids", ColumnDataType = StaticConfig.CodeFirst_BigString)]
        public List<string> ApproverIds { get; set; }


        /// <summary>
        /// 已经审批完成用户Ids 这里只可能审批同意的人，不同意的都驳回了
        /// </summary>
        [NastyColumn(ColumnName = "CompleteApproverIds", IsJson = true, IsNullable = true, ColumnDescription = "已经审批完成用户Ids", ColumnDataType = StaticConfig.CodeFirst_BigString)]
        public List<string> CompleteApproverIds { get; set; }

        /// <summary>
        /// 是否能驳回
        /// </summary>
        [NastyColumn(IsIgnore = true)]
        public bool IsCanReject
        {
            get
            {
                return !string.IsNullOrEmpty(this.RejectNodeCode);
            }
        }

        public override void OnPreAdd()
        {
            base.OnPreAdd();
            var db = AppSession.CurrentDb.Value;
            this.CompleteApproverIds = new List<string>();
        }

        public override void OnPreUpdate()
        {
            base.OnPreUpdate();

        }
    }
}
